<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />

			<cl-flex1 />
			<!-- 关键字搜索 -->
			<cl-search-key placeholder="搜索关键字" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table">
				<template #slot-status="{ scope }">
					<el-button text type="primary" bg @click="changeStatus(scope.row)">
						{{ scope.row.status == 0 ? '启用' : '禁用' }}
					</el-button>
				</template>
			</cl-table>
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" name="section-info" setup>
import { useCrud, useTable, useUpsert } from '@cool-vue/crud';
import { useCool } from '/@/cool';
import { reactive } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import type { Action } from 'element-plus';
const { service } = useCool();

// 选项
const options = reactive({
	status: [
		{ label: '禁用', value: 0, type: 'danger' },
		{ label: '启用', value: 1, type: 'success' }
	]
});

// cl-upsert
const Upsert = useUpsert({
	items: [
		{
			label: '科室名称',
			prop: 'name',
			component: { name: 'el-input', props: { clearable: true } },
			required: true
		},
		{
			label: '科室类型',
			prop: 'type',
			component: { name: 'el-input', props: { clearable: true } },
			required: true
		},
		{
			label: '负责人',
			prop: 'leader',
			component: { name: 'el-input', props: { clearable: true } },
			required: true
		},
		{
			label: '联系电话',
			prop: 'phone',
			component: { name: 'el-input', props: { clearable: true } },
			required: true
		},
		{
			label: '地址',
			prop: 'address',
			component: { name: 'el-input', props: { clearable: true } },
			required: true
		},
		{
			label: '备注',
			prop: 'remark',
			component: { name: 'el-input', props: { type: 'textarea', rows: 4 } }
		},
		{
			label: '状态',
			prop: 'status',
			component: { name: 'el-radio-group', options: options.status },
			value: 0,
			required: true
		},
		{
			label: '所属医院',
			prop: 'hospital',
			component: { name: 'el-input', props: { clearable: true } },
			required: true
		}
	]
});

// cl-table
const Table = useTable({
	columns: [
		{ type: 'selection' },
		{ label: '科室名称', prop: 'name', minWidth: 140 },
		{ label: '科室类型', prop: 'type', minWidth: 140 },
		{ label: '负责人', prop: 'leader', minWidth: 140 },
		{ label: '联系电话', prop: 'phone', minWidth: 140 },
		{ label: '地址', prop: 'address', minWidth: 140 },
		{ label: '备注', prop: 'remark', showOverflowTooltip: true, minWidth: 200 },
		{ label: '状态', prop: 'status', dict: options.status, minWidth: 120 },
		{ label: '所属医院', prop: 'hospital', minWidth: 140 },
		{
			label: '创建时间',
			prop: 'createTime',
			minWidth: 170,
			sortable: 'desc',
			component: { name: 'cl-date-text' }
		},
		{
			label: '更新时间',
			prop: 'updateTime',
			minWidth: 170,
			sortable: 'custom',
			component: { name: 'cl-date-text' }
		},
		{
			width: 240,
			type: 'op',
			buttons: ['slot-status', 'edit', 'delete']
		}
	]
});

const changeStatus = row => {
	ElMessageBox.alert('确定修改商品状态吗', '提示', {
		// if you want to disable its autofocus
		// autofocus: false,
		showCancelButton: true,
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		callback: (action: Action) => {
			service.section.info
				.changeStatus({
					id: row.id,
					status: row.status == 0 ? 1 : 0
				})
				.then(() => {
					if (action == 'confirm') {
						ElMessage({
							type: 'success',
							message: `修改成功`
						});
						Crud.value?.refresh();
					}
				})
				.catch(err => {
					ElMessage({
						type: 'error',
						message: err
					});
				});
		}
	});
};
// cl-crud
const Crud = useCrud(
	{
		service: service.section.info
	},
	app => {
		app.refresh();
	}
);

// 刷新
function refresh(params?: any) {
	Crud.value?.refresh(params);
}
</script>
